# coding:UTF-8
import time

import numpy as np
from pandas import DataFrame

from .abstract_algorithm import AbstractAlgorithm
from .abstract_algorithm_factory import AbstractAlgorithmFactory
from .abstract_config import SingleSystemHttpConfig, FaultCode


class JiareqiConf(SingleSystemHttpConfig):
    changing_room_max_temp = 10

    def select_columns(self) -> list:
        return ['sn', 'timestamp']

    def select_days(self) -> int:
        return 1

    def fault_code(self) -> FaultCode:
        return FaultCode.FRIDGE_ZHILENG


class JiareqiAlgorithm(AbstractAlgorithm[JiareqiConf]):

    def run(self, dataframe: DataFrame) -> list:
        return self.jiareqi_guzhang(dataframe)

    def jiareqi_guzhang(self, dataframe):
        return [['sn001', '2024-01-01 11:11:11', '2024-01-02 12:12:12']]
        # if len(dataframe) > 0:
        #     m = dataframe
        #     m = m.loc[:, ['sn', 'timestamp', 'freezeroomsensortempreture', 'compressorstatus', 'freezeheat',
        #                   'freezeroomevaporationsensortempreture', 'colddoor', 'changingdoor', 'freezedoorb',
        #                   'freezedoora']]
        #     m = m.fillna(0)
        #     data = m
        #     m1 = data.loc[:, ["sn"]]  # zzfld00000b sn
        #     sn_indexs = m1.drop_duplicates()["sn"]  # zzfld00000b sn
        #     error_file = []
        #     for index in sn_indexs:
        #         # print("sn=========================================================:", index)
        #         data0 = m[(m['sn'] == index)]
        #         data0 = data0.drop_duplicates(keep='last', subset=['timestamp'])
        #         # data = data0.sort_values(by='timestamp', ascending=True)
        #         data = data0.reset_index(drop=True)
        #         f_list1 = []
        #         cnt0 = 0
        #         for i in range(0, len(data) - 1):
        #             if data.iloc[i].values[4] == True and (
        #                     data.iloc[i].values[6] == 0 and data.iloc[i].values[7] == 0 and data.iloc[i].values[
        #                 8] == 0 and data.iloc[i].values[9] == 0):
        #                 f_list1.append(data.iloc[i, [1, 4, 5]].values)
        #             else:
        #                 if len(f_list1) > 5:
        #                     f_data1 = np.array(f_list1)
        #                     # f_time_l = (datetime.datetime.strptime(f_data1[:, 0][-2],"%Y-%m-%d %H:%M:%S") - datetime.datetime.strptime(f_data1[:, 0][0],"%Y-%m-%d %H:%M:%S")).seconds / 3600
        #                     # print ("===============================")
        #                     # print (np.max(f_data1[:, 2]).dtype)
        #                     if np.max(f_data1[:, 2].astype(float)) < 0:  # >=(f_data1.shape[0]* ratio ):
        #                         cnt0 += 1
        #                         error_file.append(
        #                             [index, f_data1[:, 0][0], f_data1[:, 0][-1], "freezeHeat_Fault", "http",
        #                              time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
        #                              f_data1[:, 0][-1].split(' ')[0], self.config.version()])
        #                 f_list1 = []
        #     return error_file
        # else:
        #     return []


class JiareqiFactory(AbstractAlgorithmFactory):

    def build_algorithm(self, json) -> AbstractAlgorithm:
        conf = JiareqiConf(json)
        return JiareqiAlgorithm(conf)
